package cn.main.dao.impl;

import cn.main.dao.DomainDao;
import cn.main.entity.sys.SysColumn;
import org.hibernate.Query;
import org.hibernate.internal.IteratorImpl;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Author: chengwei
 * Date:   2016/7/4
 * Time:   17:45
 */
@Repository
public class DomainDaoImpl extends BaseDaoImpl implements DomainDao {

    @Resource
    private HibernateTemplate hibernate;

    @Override
    public void setCacheQueries(boolean b) {
        hibernate.setCacheQueries(b);
    }

    @Override
    public <T> T getDomain(String id,Class<T> clazz) {
        hibernate.setCacheQueries(true);
        return hibernate.get(clazz,id);
    }

    @Override
    public <T> T getDomain(String hql, Object... args) {
        
        return null;
    }

    @Override
    public <T> void saveDomain(T t) {
        hibernate.saveOrUpdate(t);
    }

    @Override
    public <T> List<T> getDomains(Class<T> clazz, Map<String, Object> where) {
        StringBuilder hql = new StringBuilder("FROM ");
        hql.append(clazz.getName());
        List<Object> args = new ArrayList<>();
        if (where != null && where.size() > 0) {
            hql.append(" WHERE 1 = 1");
            for (String key : where.keySet()) {
                hql.append(" AND ").append(key).append(" = ?");
                args.add(where.get(key));
            }
        }
        
        List<T> result = getCurrentSession().createQuery(hql.toString()).setProperties(where).list();
        return result;
//        List<T> list = new ArrayList<>();
//        while (result.hasNext()) {
//            Object o = result.next();
//            if (o.getClass().isAssignableFrom(clazz)) {
//                list.add((T) o);
//            }
//        }
//        return  list;
    }

    @Override
    public <T> List<T> getDomains(Class<T> clazz) {
        return hibernate.loadAll(clazz);
    }

    @Override
    public <T> List<T> getDomains(Class<T> clazz,String where, Object... args) {
        List<T> list = new ArrayList<>();
        String hql = "FROM " + clazz.getName();
        hql += " " + where;
        Query query = getCurrentSession().createQuery(hql);
        for (int i = 0; i < args.length;i++) {
            query.setParameter(i,args[i]);
        }
        List<?> result = query.list();
        for (Object o : result) {
            list.add((T) o);
        }
        return  list;
    }

    @Override
    public <T> List<Map<String, Object>> getListMap(Class<T> clazz, Map<String, Object> where) {
        return null;
    }

    @Override
    public <T> List<Map<String, Object>> getListMap(Class<T> clazz, Object... args) {
        return null;
    }

    @Override
    public List<Map<String, Object>> getListMap(String hql, Object... args) {
        return null;
    }

    public <T> void deleteDomain(T t) {
        hibernate.delete(t);
    }
}
